<?php
namespace application\admin\panels;
use application\utilities\Utility;
class modules extends panel {
	public $name = 'Modules';
	protected $modulePaths = array();
	protected $modules = array();
	protected $activated = array();
	protected $deactivated = array();
	
	
	protected function parseModules() {
		$activatedModules = unserialize(getOption('activated_modules'));
		if(!$activatedModules) $activatedModules = array();
		
		$this->modulePaths = array();
		$this->modules = array();
		$this->activated = array();
		$this->deactivated = array();
		
		if ($handle = opendir(CN_DOCUMENT_ROOT . 'modules/')) {
	   	 	/* This is the correct way to loop over the directory. */
	    	while (false !== ($file = readdir($handle))) {
	       		if(is_dir(CN_DOCUMENT_ROOT . 'modules/'.$file) && $file != '.' && $file != '..') {
	       			if(file_exists(CN_DOCUMENT_ROOT . 'modules/'.$file.'/info.ini')) {
	       				$this->modulePaths[] = CN_DOCUMENT_ROOT . 'modules/'.$file.'/info.ini';
	       			}
	       		}
	    	}		
	    	closedir($handle);
		}
		
		foreach($this->modulePaths as $module) {
			$array = parse_ini_file($module);
			$this->modules[] = $array;
		}
		
		foreach($this->modules as $key=>$module) {
			if(in_array($module['File'], $activatedModules)) {
				$this->activated[] = $module;
			}
			else {
				$this->deactivated[] = $module;
			}
		}
	}
	
	public function __construct() {
		$this->parseModules();
	}
	
	public function title() {
		return 'Manage Modules';
	}
	
	public function activateModule() {
		$module = $_REQUEST['module'];
		$activatedModules = unserialize(getOption('activated_modules'));
		if(!$activatedModules) $activatedModules = array();
		
		$activatedModules[] = $module;
		if($_REQUEST['install']) {
			require CN_DOCUMENT_ROOT . 'modules/'.$_REQUEST['install'];
		}
		
		setOption('activated_modules', serialize($activatedModules));
		$this->parseModules();
		$this->showModules();

	}
	
	public function deactivateModule() {
		$module = $_REQUEST['module'];
		$activatedModules = unserialize(getOption('activated_modules'));
		$activatedModules = Utility::arrayRemoveValue($activatedModules, $module);
		if($_REQUEST['uninstall']) {
			require CN_DOCUMENT_ROOT . 'modules/'.$_REQUEST['uninstall'];
		}
		
		setOption('activated_modules', serialize($activatedModules));
		$this->parseModules();
		$this->showModules();
	}
	
	public function handleData() {
		$activatedModules = unserialize(getOption('activated_modules'));
		if(!$activatedModules) $activatedModules = array();
		$toDeactivate = array();
		
		if ($handle = opendir(CN_DOCUMENT_ROOT . 'modules/')) {
	   	 	/* This is the correct way to loop over the directory. */
	    	while (false !== ($file = readdir($handle))) {
	       		if(is_dir(CN_DOCUMENT_ROOT . 'modules/'.$file) && $file != '.' && $file != '..') {
	       			if(file_exists(CN_DOCUMENT_ROOT . 'modules/'.$file.'/info.ini')) {
	       				$this->modulePaths[] = CN_DOCUMENT_ROOT . 'modules/'.$file.'/info.ini';
	       			}
	       		}
	    	}		
	    	closedir($handle);
		}
		
		foreach($this->modulePaths as $module) {
			$array = parse_ini_file($module);
			if($_REQUEST['file'] == $array['File']) {
				if($_REQUEST['activate']) {
					$activatedModules[] = $array['File'];
					/** Call the install file */
					if($array['Install File'])
						require CN_DOCUMENT_ROOT . 'modules/'.$array['Install File'];
					
					$this->addMessage('success', 'Module activated successfully.');
				}
				elseif($_REQUEST['deactivate']) {
					$activatedModules = Utility::arrayRemoveValue($activatedModules, $array['File']);
					$this->addMessage('success', 'Module deactivated successfully.');
				}
			}
			$this->modules[] = $array;
		}
		setOption('activated_modules', serialize($activatedModules));
		
		foreach($this->modules as $key=>$module) {
			if(in_array($module['File'], $activatedModules)) {
				$this->activated[] = $module;
			}
			else {
				$this->deactivated[] = $module;
			}
		}
	
	}
	
	public function sidebar() {
		$return = array(
			array('title'=>'Activate/Deactivate','sectionID'=>'activate')
		);
		foreach(Modulator()->registeredPanels as $panel=>$info) {
			$return[] = array('title'=>$panel, 'sectionID'=>\application\utilities\Utility::makeUrl($panel));
		}
		
		return $return;
	}
	
	public function showModules() {
		?>
		<div class="box">
			<h3>Activate/Deactivate Modules</h3>
			<div class="box-content">
				<div class="two-column-left">
					<h4>Activated Modules</h4>
					<ul class="box-list">
						<?php $style = ''; ?>
						<?php foreach($this->activated as $module) : ?>
						
						<li<?=$style?>>
							<h5><b><?=$module['Title']?></b> by <b><?=$module['Author']?></b></h5>
							<p><?=$module['Description']?></p>
							<p>Version: <?=$module['Version']?></p>
							<p>
								<button data-module="<?=$module['File']?>" data-uninstall="<?=$module['Uninstall']?>" class="small-button deactivate-module">Deactivate</button>
							</p>
						</li>
						<?php $style = ($style == ' class="alternate"') ? '' : ' class="alternate"';?>
						<?php endforeach;?>
					</ul>
				</div>
				<div class="two-column-right">
					<h4>Deactivated Modules</h4>
					<ul class="box-list">
						<?php $style = ''; ?>
						<?php foreach($this->deactivated as $module) : ?>
						
						<li<?=$style?>>
							<h5><b><?=$module['Title']?></b> by <b><?=$module['Author']?></b></h5>
							<p><?=$module['Description']?></p>
							<p>Version: <?=$module['Version']?></p>
							<p>
								<button data-module="<?=$module['File']?>" data-install="<?=$module['Install']?>" class="small-button activate-module">Activate</button>
							</p>
						</li>
						<?php $style = ($style == ' class="alternate"') ? '' : ' class="alternate"';?>
						<?php endforeach;?>
					</ul>
				</div>
				<div class="clearboth"></div>
			</div>
		</div>
		<?php
	}
	
	public function display() {
	?>
	<script type="text/javascript">
	$(document).ready(function() {
		$('.deactivate-module').live('click', function() {
			$.ajax({
				url:concerto.admin.rootUrl + 'admin/?mode=ajax-method&display=modules&method=deactivateModule',
				type:'POST',
				data:{
					module:$(this).attr('data-module'),
					uninstall:$(this).attr('data-uninstall')
				},
				beforeSend:function() {
					torchd.ui.showDialog({
						type:'ajax-loader',
						title:'Deactivating...'
					});
				},
				success:function(response) {
					torchd.ui.closeAllDialogs();
					$('#activate').html(response);
				}
			});
		});
		$('.activate-module').live('click', function() {
			$.ajax({
				url:concerto.admin.rootUrl + 'admin/?mode=ajax-method&display=modules&method=activateModule',
				type:'POST',
				data:{
					module:$(this).attr('data-module'),
					install:$(this).attr('data-install')
				},
				beforeSend:function() {
					torchd.ui.showDialog({
						type:'ajax-loader',
						title:'Activating...'
					});
				},
				success:function(response) {
					torchd.ui.closeAllDialogs();
					$('#activate').html(response);
				}
			});
		});
	});
	</script>
		<section id="activate">
		<?php $this->showModules();?>
		</section>
		<?php foreach(Modulator()->registeredPanels as $panel=>$function) : ?>
			
		
			<section id="<?=\application\utilities\Utility::makeUrl($panel)?>" style="display:none;">
				<div class="box">
					<h3><?=$panel?></h3>
					<div class="box-content">
						<?php $function();?>
					</div>
				</div>
			</section>
		<?php endforeach;?>		
		<?php
	}
}
?>